var rootPath = getRootPath();
var dataformInit
var jsonTextInit

$(function() {

	preview(this)
	var url = location.search;

	$.ajax({
		url : rootPath + '/selectproduct',
		type : 'POST',
		data : {
			"id" : GetQueryString("uid"),
		},
		success : function(data) {
			help_show(data);

		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error:(");
		}
	});

	$("#admin-help-edit").click(function() {
		
		var dataform = $("#helpEditForm").serializeArray();		
		var jsonText = JSON.stringify({
			dataform : dataform
		});
		var zswb = $("#logoFile").val();
		
console.log(JSON.stringify( transformToJson(dataform)))
		if (jsonTextInit == jsonText & zswb == '') {
			alert("表单值没有改变！");
			return false;
		} else {			
			if(zswb==''){
			
				$.ajax({
					url : rootPath + '/updateproduce',
					type : 'POST',
					data : JSON.stringify( transformToJson(dataform)),
					dataType: 'json',
					contentType:"application/json;charset=UTF-8",
					success : function(data) {
						
						layer.msg('成功',{icon:1,time:1000},function() {
							parent.window.location.href = rootPath + '/helpPage';
							var index = parent.layer.getFrameIndex(window.name);
							parent.layer.close(index);
						});
						//layer.alert("成功");

					},
					error : function(jqXHR, textStatus, errorThrown) {
						layer.alert("失败");
					}
				});

			}else{
				updateimg()
				var dataform = $("#helpEditForm").serializeArray();
				$.ajax({
					url : rootPath + '/updateproduce',
					type : 'POST',
					data : JSON.stringify( transformToJson(dataform)),
					dataType: 'json',
					contentType:"application/json;charset=UTF-8",
					success : function(data) {
						layer.alert("成功");

					},
					error : function(jqXHR, textStatus, errorThrown) {
						layer.alert("error:(");
					}
				});
			}
			
			
			return false;
		}
	})

})


function updateimg(){	
	var formData = new FormData($("#helpAddForm")[0]); 		
	 formData.append("file",$("#logoFile")[0].files[0])
	 console.log(formData);		
	$.ajax({
		url:rootPath+"/uploadImg",
		type:"POST",
		data:formData,
		contentType:false,
		processData:false,
		async : false,
		success:function(data){
			document.getElementById("imgurl").value = data;   			
		},
		error: function () {
			console.log("失败"); 
        }
	
	});
	
}



function transformToJson(formData){
    var obj={}
    for (var i in formData) {
        obj[formData[i].name]=formData[i]['value'];
    }
    return obj;
}


function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);// search,查询？后面的参数，并匹配正则
	if (r != null)
		return unescape(r[2]);
	return null;

}

function help_show(data) {
	console.log(data.dataObject)
	var prevDiv = document.getElementById('preview');
	prevDiv.innerHTML = '<img  style="width: 100px;height:100px;" src='+rootPath+'/file/viewimg?fileName='
			+ data.dataObject.imgurl + '&type=4 />';
	document.getElementById("helpid").value = data.dataObject.id;
	document.getElementById("productid").value = data.dataObject.productid;
	document.getElementById("name").value = data.dataObject.name;
	document.getElementById("tucaourl").value = data.dataObject.tucaourl;
	document.getElementById("help_select").value = data.dataObject.sort;
	dataformInit = $("#helpEditForm").serializeArray();
	jsonTextInit = JSON.stringify({
		dataform : dataformInit
	});

	console.log(jsonTextInit)
}

function preview(file) {
	var zswb = $("#logoFile").val();
	console.log(zswb)
	console.log(file)
	$("#imgHidden").css("display", "none");
	var prevDiv = document.getElementById('preview');
	if (file.files && file.files[0]) {
		var reader = new FileReader();
		reader.onload = function(evt) {
			prevDiv.innerHTML = '<img style="width: 100px;height: 100px;" src="'
					+ evt.target.result + '" />';
		}
		reader.readAsDataURL(file.files[0]);
	}
}